package leetcodev1.数组;

public class LeetCode34 {

    //二分查找拓展
    //找到target的第一个值，依次遍历即可
    public int[] searchRange(int[] nums, int target) {
        if (nums == null || nums.length == 0) {
            return new int[]{-1, -1};
        }

        int left = 0, right = nums.length - 1, startIndex = -1, endIndex;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                startIndex = mid;
            }

            if (nums[mid] >= target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        if (startIndex == -1) {
            return new int[]{-1, -1};
        }
        endIndex = startIndex + 1;
        while (endIndex < nums.length && nums[endIndex] == target) {
            endIndex++;
        }
        return new int[]{startIndex, endIndex - 1};
    }
}
